package com.bbs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bbs.bean.UserInfo;
import com.bbs.util.MysqlDB;

/** 
 * @className: ControllerUserLogout.java
 * @classDescription: 
 * @function: 
 * @author: Wentasy
 * @createTime: 2012-12-11 下午08:13:57
 * @modifyTime: 
 * @modifyReason: 
 * @since: JDK 1.6
 */
public class ControllerUserLogout extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ControllerUserLogout() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int userId = Integer.parseInt(request.getParameter("userId").trim());
		String backNews = "";
		Connection conn = null;
		Statement stmt = null;
		String sql = "";
		ResultSet rs = null;
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession(true);
		int userType=0;
		if (session.getAttribute("userType") != null) {
			userType = (Integer) session.getAttribute("userType");
		}
		
		if(userType==1 || userType==2 ){
			List<UserInfo> lists = new ArrayList<UserInfo>();
			try {
				conn = MysqlDB.getConnection();
				stmt = MysqlDB.createStatement(conn);
				sql = "select * from user order by userType desc,userid";
				rs = MysqlDB.executeQuery(stmt, sql);
	
				while (rs.next()) {
					UserInfo userList = new UserInfo();
					userList.setBackNews("操作成功");
					userList.setAge(rs.getInt("age"));
					userList.setEmail(rs.getString("email"));
					userList.setRegdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rs.getTimestamp("regdate")));
					userList.setSex(rs.getInt("sex"));
					userList.setUserid(rs.getInt("userId"));
					userList.setPassword(rs.getString("password"));
					userList.setUsername(rs.getString("username"));
					userList.setUserType(rs.getInt("userType"));
					lists.add(userList);
				}
				
				conn = MysqlDB.getConnection();
				stmt = MysqlDB.createStatement(conn);
				sql = "update user set userType = '-1' where userId="+userId;
				int rsR = MysqlDB.executeUpdate(stmt, sql);
				
				if (rsR != 0) {
					backNews = "操作成功";
				} else {
					backNews = "操作失败";
				}
				request.setAttribute("userList", lists);
				
				RequestDispatcher dispatcher = request.getRequestDispatcher("pages/viewContent.jsp");
				dispatcher.forward(request, response);
			}catch (Exception e) {
				e.printStackTrace();
				System.out.println(backNews);
			}finally{
				MysqlDB.close(rs);
				MysqlDB.close(stmt);
				MysqlDB.close(conn);
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
